// pages/my/my.js
import {Address} from '../../pages/order/address-model.js'
import {Order} from '../../pages/order/order-model.js'
var order = new Order();
var address = new Address();
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasUserInfo:false,
    pageIndex:1,
    orderArr:[],
    isloadedAll:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._getAddressInfo();
    this._getOrders();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 每次加载页面都判断是否有新的订单，如果有，则重新加载数据
    if (order.hasNewOrder()){
      this._refresh();
    }
  },

  /** 
   * 订单数据初始化，重新刷新
   */
  _refresh(){
    this.data.orderArr = [];
    this.data.pageIndex = 1;
    this.data.isloadedAll = false;
    this._getOrders();
    order.execSetStorageSync(false);
  },

  /**
   * 编辑用户收货地址
   * @param {*} event 
   */
  editAddress:function(event) {
    var that = this;
    wx.chooseAddress({
      success: (result) => {
        var addressInfo = {
          name: result.userName,
          mobile: result.telNumber,
          totalDetail: address.setAddressInfo(result)
        }

        this.setData({
          addressInfo: addressInfo
        });
        // 修改用户所属的收货地址

        address.submitAddress(result,(flag,res) => {
          if (!flag) {
            that._showTips('操作提示', '地址信息更新失败');
          }
        })
      },
    })
  },

  /**
   * 获取订单详情
   */
  showOrderDetailInfo:function(event) {
    var id = order.getDataSet(event,'id');
    wx.navigateTo({
      url: '../order/order?from=order&id=' + id,
    })
  },

  /**
  * 获取我的订单列表数据
  */
 _getOrders:function () {
  order.getOrderList(this.data.pageIndex, (res) => {
    var list = res.data;

    if (list.length > 0) {
      this.data.orderArr.push.apply(this.data.orderArr, list);
      this.setData({
        orderArr:this.data.orderArr
      })
    }else {
      this.data.isloadedAll = true;
    }
  })
 },

  /**
   * 获取用户地址信息
   */
  _getAddressInfo:function(){
    address.getUserAddressInfo((res) => {
     this.setData({
       addressInfo: res
     })
   })
 },

 /**
  * 滚动倒底部触发
  */
 onReachBottom:function(){
  if(!this.data.isloadedAll){
    this.data.pageIndex++;
    this._getOrders();
  }
},

rePay:function(event) {
  var id = order.getDataSet(event,'id'),
      index = order.getDataSet(event,'index');

      if(!order.onPay) {
        this._showTips('支付提示','本产品仅用于演示，支付系统已屏蔽',false);//屏蔽支付，提示
        
        // 假装支付成功

      }else{
        order.execPay(id, (orderStatus) => {
          if (orderStatus > 0){
            var flag = orderStatus == 2;
            if (flag){
              that.data.orderArr[index].status=2;
              that.setData({
                  orderArr: that.data.orderArr
              });
            }
            //跳转到 成功页面
            wx.navigateTo({
                url: '../pay-result/pay-result?id='+id+'&flag='+flag+'&from=my'
            });
  
          }else {
            this._showTips('支付失败','商品已下架或库存不足');
          }
        })
      }
},

  /*
   * 提示窗口
   * params:
   * title - {string}标题
   * content - {string}内容
   * flag - {bool}是否跳转到 "我的页面"
   */
  _showTips:function(title,content){
    wx.showModal({
        title: title,
        content: content,
        showCancel:false,
        success: function(res) {

        }
    });
},
})